package com.qf.user.service.impl;

import com.qf.base.utils.PatternUtil;
import com.qf.user.dao.UserDao;
import com.qf.user.dao.UserMapper;
import com.qf.user.model.User;
import com.qf.user.model.UserExample;
import com.qf.user.service.UserService;
import com.qf.user.settings.Settings;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * projectName: automation
 *
 * @author dongshihao
 * Date:2021/9/26   23:00
 * description:
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;
    @Resource
    private Settings settings;

    @Resource
    private UserMapper userMapper;

    @Override
    public User signIn(String signName, String password) {
        User user = isLocked(signName);
        if (user == null) {
            //登录名不在数据库中
            return null;
        }
        if (user.getIsLocked() == 1) {
            //已经被锁定了
            return user;
        }
        if (user.getPassword().equals(password)) {
            //密码相同
            return user;
        } else {
            //密码错误
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
            simpleDateFormat.applyPattern("yyyyMMdd");

            Date lockedTime = user.getLockedTime();
            if (lockedTime == null) {
                //从1 记起
                user.setIsLocked(0);
                user.setTryCount(0);
                //持久化到数据库
                userDao.updateByPrimaryKeySelective(user);
                return null;
            }
            //取出数据库里的这一天
            String dbDay = simpleDateFormat.format(lockedTime);
            //取今天
            String nowDay = simpleDateFormat.format(new Date());

            if (dbDay.equals(nowDay)) {
                //累计次数
                user.setTryCount(user.getTryCount() + 1);
                Integer count = Integer.parseInt(settings.get(Settings.TRY_MAX_COUNT));
                if (user.getTryCount().equals(count)) {
                    //锁定
                    user.setIsLocked(1);
                }
            } else {
                user.setLockedTime(new Date());
                user.setTryCount(1);
            }
        }
        //持久化到数据库
        userDao.updateByPrimaryKeySelective(user);
        return null;
    }


    @Override
    public User isLocked(String signName) {
        //插件自动生成的sql语句
        //包含例子对象，条件对象
        //查询应该是 根据例子对象查询
        //例子对象可以设置好多对象，通过条件对象设置；
        UserExample example = new UserExample();
        //基于例子对象产生的条件对象
        UserExample.Criteria criteria = example.createCriteria();

        boolean isMobile = PatternUtil.isMobile(signName);
        if (isMobile) {
            //手机号
            //设置条件为手机号等于登录名
            criteria.andTelephoneEqualTo(signName);
        } else {

            boolean isEmail = PatternUtil.isEmail(signName);
            if (isEmail) {
                //邮箱
                //邮箱等于登录名
                criteria.andEmailEqualTo(signName);
            } else {
                //用户名
                //设置用户名为登录名
                criteria.andUsernameEqualTo(signName);
            }
        }
        //查询数据库
        List<User> users = userDao.selectByExample(example);
        System.out.println("users = " + users.toString());
        if (users != null && users.size() > 0) {
            //找到用户
            User user = users.get(0);
            //判读是否被锁定

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
            Date lockedTime = user.getLockedTime();
            if (lockedTime != null) {
                //至少曾经锁过
                //数据库里存的锁定的日期
                String dbDay = simpleDateFormat.format(lockedTime);
                String newDay = simpleDateFormat.format(new Date());
                if (!dbDay.equals(newDay)) {
                    //没锁住
                    user.setIsLocked(0);
                    user.setTryCount(0);
                }
//                新数据库。设置为不锁
                userDao.updateByPrimaryKeySelective(user);
            } else {
                //没锁
                user.setIsLocked(0);
                user.setTryCount(0);
                //更新数据库
                userDao.updateByPrimaryKeySelective(user);
            }
            System.out.println("user = " + user.toString());
            return user;
        }
        return null;
    }


    @Override
    public User oneKeySignIn(String phone) {
        //1，用手机查询用户
        // 查到了就返回用户
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andTelephoneEqualTo(phone);
        List<User> users = userDao.selectByExample(example);
        if (users != null && users.size() > 0) {
            return users.get(0);
        } else {
            //2. 没查到就帮用户注册用一个用户
            //然后返回这个用户
            User user = new User();
            //设置信息
            user.setTelephone(phone);
            //自动生成昵称 id
            user.setNickname(userMapper.randomNickName());
            //插入数据
            userMapper.insertUserForId(user);
            return user;
        }
    }

    @Override
    public User findUserById(Long userId) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(userId);
        List<User> users = userDao.selectByExample(example);
        if (users != null && users.size() > 0) {
            return users.get(0);
        }

        return null;
    }
}
